﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DingzhiZhengdingQuUIController : MonoBehaviour {

    public static DingzhiZhengdingQuUIController _instance;
    private int i_left;                        //左边文字选择框的索引值
    private int i_right=-1;                       //右边数字选择框的索引值
    private Color32 c_hitColor = new Color32(255, 241, 255, 255);       //高亮状态
    private Color32 c_notHitColor = new Color32(230, 67, 255, 255);     
    private int i_AllUIChildCount;             //ALLUI的child的数量
    private float f_ContentIndex = 1.0f;       //定值整定中内容的变化值
    private string s_dir="left";               //当前选择框所在的位置（left  OR   right）
    public Transform tf_AllUI;                 
    public Transform tf_ContentRight;           //定值整定数字区
    public Transform tf_ContentLeft;            //定值整定文字区
    public Scrollbar sb_Content;
    public Text t_bottomUI;                     //界面底部的文字
    private int i_tempValue;                    //判断ScrollBar的value能否变化

    // Use this for initialization
    void Start () {
        _instance = this;
        i_AllUIChildCount = tf_AllUI.childCount;
        EventManager<bool>.AddEvent(MyEventType.UpButtonClickEvent, OnUpButtonClick);
        EventManager<bool>.AddEvent(MyEventType.DownButtonClickEvent, OnDownButtonClick);
        EventManager<bool>.AddEvent(MyEventType.LeftButtonClickEvent, OnLeftButtonClick);
        EventManager<bool>.AddEvent(MyEventType.RightButtonClickEvent, OnRightButtonClick);
        EventManager<bool>.AddEvent(MyEventType.SetButtonClickEvent, OnSetButtonClick);
        EventManager<bool>.AddEvent(MyEventType.QuitButtonClickEvent, OnQuitButtonClick);
        EventManager<bool>.AddEvent(MyEventType.ChongzhiShujuEvent, Reset);
    }

    private void Reset(bool obj)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "dingzhizhengdingQuGO") {
            if (i_right >= 0) {
                tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(0).gameObject.SetActive(false);
            }
            UIController._instance.ChangeGaoliang(tf_ContentLeft, i_left, false);
            i_left = 0;
            i_right = -1;
            s_dir = "left";
            textChange(i_left);
            f_ContentIndex = 1;
            i_tempValue = 0;
            sb_Content.value = f_ContentIndex;
            UIController._instance.ChangeGaoliang(tf_ContentLeft, i_left, true);
        }
    }

    private void OnQuitButtonClick(bool obj)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "dingzhizhengdingQuGO")
        {
            for (int i = 0; i < i_AllUIChildCount; i++)
            {
                if (tf_AllUI.GetChild(i).name == "dingzhizhengdingGO")
                {
                    
                    TimerMngr.Instance.Create(() =>
                    {
                        EventManager<bool>.TriggerEvent(MyEventType.ChongzhiShujuEvent, true);
                        tf_AllUI.GetChild(i).SetAsLastSibling();
                    }).Start(0.1f);

                    break;
                }
            }
        }
    }
    private void OnSetButtonClick(bool obj)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "dingzhizhengdingQuGO")
        {
            for (int i = 0; i < i_AllUIChildCount; i++)
            {
                if (tf_AllUI.GetChild(i).name == "dingzhiguhuaGO")
                {
                    
                    TimerMngr.Instance.Create(() =>
                    {
                        EventManager<bool>.TriggerEvent(MyEventType.ChongzhiShujuEvent, true);
                        tf_AllUI.GetChild(i).SetAsLastSibling();
                    }).Start(0.1f);

                    break;
                }
            }
        }
    }
    private void OnRightButtonClick(bool obj)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "dingzhizhengdingQuGO") {
            if (i_left >= 11) {//到达实验需修改的地方才允许向右
                s_dir = "right";//设置此时的高亮区域在右边的数字区域
                UIController._instance.ChangeGaoliang(tf_ContentLeft, i_left, false); ;  //左边文字高亮取消
                i_right++;
                if (i_right > 3) {//数字为4个+1个"."（索引值最高为4）
                    i_right = 3;                                       
                }else if (i_right == 0) {
                    tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(0).gameObject.SetActive(true);//第一个数字背景高亮
                    tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(1).GetComponent<Text>().color = UIController._instance.c_hitColorText;
                }
                else if (i_right >= 1) //第一个数字高亮后
                {
                    tf_ContentRight.GetChild(i_left).Find((i_right - 1).ToString()).GetChild(1).GetComponent<Text>().color = UIController._instance.c_notHitColorText;
                    tf_ContentRight.GetChild(i_left).Find((i_right-1).ToString()).GetChild(0).gameObject.SetActive(false);//前一个数字背景高亮取消
                    tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(0).gameObject.SetActive(true);
                    tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(1).GetComponent<Text>().color = UIController._instance.c_hitColorText;
                }
            } 
        }
    }

    private void OnLeftButtonClick(bool obj)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "dingzhizhengdingQuGO")
        {
            if (i_right == 0) {//右边第一个数字高亮
                s_dir = "left";//设置此时的高亮区域在左边的数字区域
                tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(0).gameObject.SetActive(false);
                tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(1).GetComponent<Text>().color = UIController._instance.c_notHitColorText;
                UIController._instance.ChangeGaoliang(tf_ContentLeft, i_left, true); ;
                i_right--;
            }
            else if(i_right>0){//高亮区域在右边的区域并且高亮的数字级数>=2
                tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(0).gameObject.SetActive(false);
                tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(1).GetComponent<Text>().color = UIController._instance.c_notHitColorText;
                i_right--;
                tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(1).GetComponent<Text>().color = UIController._instance.c_hitColorText;
                tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(0).gameObject.SetActive(true);
            }
        }
    }
    private void OnDownButtonClick(bool obj)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "dingzhizhengdingQuGO") {
            //左边的话  只进行上下高亮操作
            if (s_dir == "left")
            {
                if (i_left < tf_ContentLeft.childCount - 1)
                {
                    UIController._instance.ChangeGaoliang(tf_ContentLeft,i_left,false);
                    i_left++;
                    i_tempValue++;
                    if (i_tempValue > 3) {
                        i_tempValue = 3;
                    }
                    UIController._instance.ChangeGaoliang(tf_ContentLeft, i_left, true);
                    if (i_tempValue == 3) {//
                        i_tempValue = 0;
                        f_ContentIndex -= 3.0f / 15.0f;
                        sb_Content.value = f_ContentIndex;
                        if (f_ContentIndex <= 0)
                        {
                            f_ContentIndex = 0;
                        }
                    }
                    textChange(i_left);
                }
            }
            //右边的话 高亮+数字的增减
            else if (s_dir == "right")
            {
                if (i_left == 14 || i_left == 16)
                {
                    if (float.Parse(ShijianJiluUIController._instance.PinjieDianliu(i_left)) <= 0.1f)
                    {
                        return;
                    }
                }
                else if (i_left == 11 || i_left == 13 || i_left == 15)
                {
                    if (float.Parse(ShijianJiluUIController._instance.PinjieDianliu(i_left)) <= 0.25f)
                    {
                        return;
                    }
                }
                int temp = int.Parse(tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(1).GetComponent<Text>().text);
                temp--;
                int index = tf_ContentRight.GetChild(i_left).Find(".").GetSiblingIndex();//“.”的层级
                if (index == 1)
                {
                    if (temp < 0)
                    {
                        temp = 0;
                    }
                    tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(1).GetComponent<Text>().text = temp.ToString();
                }
                else if (index == 2)
                {
                    if (temp < 0)
                    {
                        if (i_right == 1)//当前在第二个数字
                        {
                            int temp0 = int.Parse(tf_ContentRight.GetChild(i_left).Find(0.ToString()).GetChild(1).GetComponent<Text>().text);//取得第一个数字
                            if (temp0 <= 0)
                            {
                                temp0 = 0;
                            }
                            if (temp0 == 0)
                            {
                                tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(1).GetComponent<Text>().text = 0.ToString();
                            }
                            else
                            {
                                temp0--;
                                tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(1).GetComponent<Text>().text = 9.ToString();
                            }
                            tf_ContentRight.GetChild(i_left).Find(0.ToString()).GetChild(1).GetComponent<Text>().text = temp0.ToString();
                        }
                        else if (i_right == 0)//当前在第一个数字
                        {
                            if (temp <= 0)
                            {
                                temp = 0;
                            }
                            tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(1).GetComponent<Text>().text = temp.ToString();
                        }
                        else {
                            if (temp <= 0)
                            {
                                temp = 0;
                            }
                            tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(1).GetComponent<Text>().text = temp.ToString();
                        }
                    }
                    else //temp>=0
                        tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(1).GetComponent<Text>().text = temp.ToString();
                }
                else if (index == 3) {//100
                    if (i_right == 2) {//光标在个位数才能进行
                        tf_ContentRight.GetChild(i_left).Find(".").SetSiblingIndex(2);
                        tf_ContentRight.GetChild(i_left).Find(2.ToString()).GetChild(1).GetComponent<Text>().color = UIController._instance.c_notHitColorText;
                        tf_ContentRight.GetChild(i_left).Find(2.ToString()).GetChild(0).gameObject.SetActive(false);
                        tf_ContentRight.GetChild(i_left).Find(1.ToString()).GetChild(1).GetComponent<Text>().color = UIController._instance.c_hitColorText;
                        tf_ContentRight.GetChild(i_left).Find(1.ToString()).GetChild(0).gameObject.SetActive(true);
                        i_right = 1;
                        tf_ContentRight.GetChild(i_left).Find(0.ToString()).GetChild(1).GetComponent<Text>().text = 9.ToString();
                        tf_ContentRight.GetChild(i_left).Find(1.ToString()).GetChild(1).GetComponent<Text>().text = 9.ToString();
                    }    
                }
                if (isAllZero())
                {
                    tf_ContentRight.GetChild(i_left).Find(".").SetSiblingIndex(1);
                }
            }
        } 
    }

    /// <summary>
    /// 判断是否为0
    /// </summary>
    /// <returns></returns>
    private bool isAllZero() {
        int num = 0;
        for (int i = 0; i < 4; i++) {
            if (tf_ContentRight.GetChild(i_left).Find(i.ToString()).GetChild(1).GetComponent<Text>().text == "0") {
                num++;
            }
        }
        if (num == 4) {
            return true;
        }
        return false;
    }

    private void OnUpButtonClick(bool obj)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "dingzhizhengdingQuGO")
        {
            //左边的话  只进行上下高亮操作
            if (s_dir == "left")
            {
                if (i_left > 0 && i_left <= tf_ContentLeft.childCount - 1)
                {
                    UIController._instance.ChangeGaoliang(tf_ContentLeft, i_left, false);
                    i_left--;
                    i_tempValue--;
                    if (i_tempValue < 0) {
                        i_tempValue = -1;
                    }
                    UIController._instance.ChangeGaoliang(tf_ContentLeft, i_left, true);
                    if (i_tempValue== -1)
                    {
                        i_tempValue = 2;
                        f_ContentIndex += 3.0f/15.0f;
                        sb_Content.value = f_ContentIndex;
                        if (f_ContentIndex >= 1)
                        {
                            f_ContentIndex = 1;
                        }
                    }
                    textChange(i_left);
                }
            }
            //右边的话 高亮+数字的增减
            else if (s_dir == "right")
            {
                if (i_left==12 || i_left == 14 || i_left == 16)
                {
                    if (float.Parse(ShijianJiluUIController._instance.PinjieDianliu(i_left)) >= 32.0f)
                    {
                        return;
                    }
                }
                else if (i_left == 11 || i_left == 13 || i_left == 15)
                {
                    if (float.Parse(ShijianJiluUIController._instance.PinjieDianliu(i_left)) >= 100.0f)
                    {
                        return;
                    }
                }
                int temp = int.Parse(tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(1).GetComponent<Text>().text);
                temp++;
                int index = tf_ContentRight.GetChild(i_left).Find(".").GetSiblingIndex();//“.”的层级
                if (index == 1)//数字<10
                {
                    if (i_right == 0)
                    {
                        if (temp > 9)
                        {
                            temp = 1;
                            tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(1).GetComponent<Text>().text = temp.ToString();
                            tf_ContentRight.GetChild(i_left).Find(".").SetSiblingIndex(2);
                            tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(0).gameObject.SetActive(false);
                            tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(1).GetComponent<Text>().color = UIController._instance.c_notHitColorText;
                            i_right++;
                            tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(1).GetComponent<Text>().color = UIController._instance.c_hitColorText;
                            tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(0).gameObject.SetActive(true);
                            tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(1).GetComponent<Text>().text = 0.ToString();
                        }
                        else {
                            tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(1).GetComponent<Text>().text = temp.ToString();
                        }  
                    }
                    else if (i_right>=1) {
                        if (temp > 9) {
                            temp = 9;
                        }
                        tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(1).GetComponent<Text>().text = temp.ToString();
                    }    
                }
                else if (index == 2)
                {
                    if (temp > 9)
                    {
                        if (i_right == 1)
                        {
                            int temp0 = int.Parse(tf_ContentRight.GetChild(i_left).Find(0.ToString()).GetChild(1).GetComponent<Text>().text);//取得第一个数字的值
                            temp0++;
                            tf_ContentRight.GetChild(i_left).Find(0.ToString()).GetChild(1).GetComponent<Text>().text = temp0.ToString();
                            tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(1).GetComponent<Text>().text = 0.ToString();
                            if (temp0 >= 9)
                            {
                                tf_ContentRight.GetChild(i_left).Find(".").SetSiblingIndex(3);
                                temp0 = 9;
                                tf_ContentRight.GetChild(i_left).Find(0.ToString()).GetChild(1).GetComponent<Text>().text = 1.ToString();
                                tf_ContentRight.GetChild(i_left).Find(1.ToString()).GetChild(1).GetComponent<Text>().text = 0.ToString();
                                tf_ContentRight.GetChild(i_left).Find(2.ToString()).GetChild(1).GetComponent<Text>().text = 0.ToString();
                                i_right = 2;
                                tf_ContentRight.GetChild(i_left).Find(2.ToString()).GetChild(1).GetComponent<Text>().color = UIController._instance.c_hitColorText;
                                tf_ContentRight.GetChild(i_left).Find(2.ToString()).GetChild(0).gameObject.SetActive(true);
                                tf_ContentRight.GetChild(i_left).Find(1.ToString()).GetChild(1).GetComponent<Text>().color = UIController._instance.c_notHitColorText;
                                tf_ContentRight.GetChild(i_left).Find(1.ToString()).GetChild(0).gameObject.SetActive(false);
                            }  
                        }
                        else if (i_right == 0)
                        {
                            if (temp >= 9)
                            {
                                temp = 9;
                            }
                            tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(1).GetComponent<Text>().text = temp.ToString();
                        }
                    }
                    else if (temp <= 9) {
                        tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(1).GetComponent<Text>().text = temp.ToString();
                    }
                }else if (index == 3)//100.0
                {
                    //tf_ContentRight.GetChild(i_left).Find(i_right.ToString()).GetChild(1).GetComponent<Text>().text = 100.ToString();
                }
            }
        }
    }

    /// <summary>
    /// 底部文字改变
    /// </summary>
    /// <param name="index"></param>
    private void textChange(int index) {
        switch (index) {
            case 0:t_bottomUI.text = "长按>键定义控制字";break;
            case 1: t_bottomUI.text = "长按>键定义控制字";break;
            case 2: t_bottomUI.text = "长按>键定义控制字"; break;
            case 3: t_bottomUI.text = ".Min=0.001  Max=20.00"; break;
            case 4: t_bottomUI.text = ".Min=0.001  Max=50.00"; break;
            case 5: t_bottomUI.text = ".Min=0.010  Max=100.00"; break;
            case 6: t_bottomUI.text = ".Min=0.010  Max=100.00"; break;
            case 7: t_bottomUI.text = ".Min=0.010  Max=100.00"; break;
            case 8: t_bottomUI.text = ".Min=0.100  Max=32.00"; break;
            case 9: t_bottomUI.text = ".Min=0.010  Max=100.00"; break;
            case 10: t_bottomUI.text = ".Min=0.100  Max=50.00"; break;
            case 11: t_bottomUI.text = ".Min=0.250  Max=100.00"; break;
            case 12: t_bottomUI.text = ".Min=0.000  Max=32.00"; break;
            case 13: t_bottomUI.text = ".Min=0.250  Max=100.00"; break;
            case 14: t_bottomUI.text = ".Min=0.100  Max=32.00"; break;
            case 15: t_bottomUI.text = ".Min=0.250  Max=100.00"; break;
            case 16: t_bottomUI.text = ".Min=0.100  Max=32.00"; break;
            case 17: t_bottomUI.text = ".Min=0.100  Max=32.00"; break;
            default: break;
        }
    }
}
